package com.snail.snailvr;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.snail.snailvr.transfer.CustomNanoHTTPD;
import com.snail.snailvr.utils.Constant;
import com.snail.snailvr.utils.DatabaseUtils;
import com.snail.snailvr.utils.DownloadObserverServiceUtil;
import com.snail.snailvr.utils.SilenceUpdateServicesUtil;

import junit.framework.Assert;

import java.io.IOException;

/**
 * 全局Service用下下载
 * Created by snail on 16/4/15.
 */
public class SnailVrService extends Service {

    private CustomNanoHTTPD transferServer;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int extra=-1;
        if(intent!=null)
            extra = intent.getIntExtra(Constant.KEY_BACKGROUND_TASK_TYPE, -1);
        switch (extra) {
            case Constant.START_WIFI_TRANS_SERVER:
                startWifiTransferServer();
                break;
            case Constant.STOP_WIFI_TRANS_SERVER:
                stopWifiTransferServer();
                break;

            case Constant.START_SLIENT_UPDATE:
                SilenceUpdateServicesUtil.getInstance(this);
                break;
            case Constant.STOP_SLIENT_UPDATE:
                SilenceUpdateServicesUtil.getInstance(this).onDestroy();
                break;
            default:
                DownloadObserverServiceUtil.getInstance(this);
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * 关闭WIFI传输服务
     */
    private void stopWifiTransferServer() {
        if (transferServer != null)
            transferServer.stop();
        DatabaseUtils.clearConnectPcList();

    }

    /**
     * 开启WIFI传输服务
     */
    private void startWifiTransferServer() {

        if (transferServer == null) {
            transferServer = new CustomNanoHTTPD();
        }

        if (!transferServer.isAlive()) {
            try {
                this.transferServer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                long start = System.currentTimeMillis();
                Thread.sleep(100L);
                while (!this.transferServer.wasStarted()) {
                    Thread.sleep(100L);
                    if (System.currentTimeMillis() - start > 2000) {
                        Assert.fail("could not start server");
                    }
                }
            } catch (InterruptedException e) {
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DownloadObserverServiceUtil.getInstance(this).onDestroy();
        SilenceUpdateServicesUtil.getInstance(this).onDestroy();
        stopWifiTransferServer();
    }
}
